<?php

/**
 * Pes for PHP 5.3+
 *
 * Copyright (c) 2013 phpeskill.com (http://www.pescms.com)
 *
 * For the full copyright and license information, please view
 * the file LICENSE.md that was distributed with this source code.
 */

namespace App\Home;

use Core\Db\Db as Db;
use Expand\Home\Page as Page;
use Core\Cache\FileCache as FileCache;

/**
 * PES前台搜索
 * @author LazyPHP
 * @version 1.0
 */
class Search extends Common {

    public function index() {
        $keyword = $this->is_g('keyword', '请填写关键词');
        $new = array();

        $searchCache = new FileCache();
        $cache = $searchCache->loadCache("search_{$keyword}");
        if ($cache == false) {
            $db = $this->db('model');
            $modelList = $db->where('is_search = 1')->select();

            foreach ($modelList as $key => $value) {
                $db->tableName(strtolower($value['model_name']));
                $result[] = $db->where('title like :title')->select(array('title' => "%{$keyword}%"));
            }
            foreach ($result as $key => $value) {
                foreach ($value as $_value) {
                    $new[] = $_value;
                }
            }
            $searchCache->createCache("search_{$keyword}", $new);
        }else{
            $new = $cache;
        }
        

        $page = new Page;
        $total = count($new);
        $count = $page->total($total);
        $page->handle();
        $list = array_slice($new, $page->firstRow, $page->listRows);
        $show = $page->show();
        $this->assign('page', $show);
        $this->assign('list', $list);
        $this->assign('cat', array('url'=>'javascript:void(0);', 'cate_name'=> "搜索'{$keyword}'"));
        $this->display('search');
    }

}